package com.tool.cn.util;

import java.math.BigInteger;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 随机数
 * @author wangzh
 * 2018年2月8日 下午4:45:01
 */
public final class RandomUtil {
	
	/**
	 * 随机数对象
	 */
	private static Random random;
	
	
	
	/**
	 * 初始化算子
	 */
	static{
		String name=SystemUtil.getHostName();
		String md5=StringUtil.passAlgorithmsCiphering(name, StringUtil.MD5);
		
		//获得算子
		BigInteger bigint=new BigInteger(md5, 16);
	    String key=bigint.toString().substring(0,19);
	    
		Long count;
		try{
		    count=Long.parseLong(key);
		}catch(NumberFormatException e){
			key=1+key.substring(1);
			count=Long.parseLong(key);
		}
		
		//生成随机数
		random = new Random(count);
	}
	
	/**
	 * int范围类的随机数
	 */
	public static int getRandom(){
		Random rand = new Random();
		return rand.nextInt(); 
	}
	
	/**
	 * 生成size范围内整数随机，从0开始，size必须大于0，当size为0时，冲定义为10
	 */
	public static int getRandom(int size){
		
		if(size<=0) {
			size=10;
		}
		
		Random rand =new Random();  //可传参指定long类型算子
		return rand.nextInt(size); 
	}
	
	/**
	 * 生成指定位数整数
	 * @param length
	 * @return
	 */
	public static int getSizeNum(int length){
		if(length>9) {  //最长不超过9
			System.out.println("Max Length Less Than Nine");
			return 0;
		}
		Random random = new Random();
		int size,add;
		if (length==1) {
			size=9;
			add=1;
		}else {
			String lengthString="1";
			for(int i=1;i<length;i++) {
				lengthString+="0";
			}
			
			add=Integer.parseInt(lengthString)+1;
			String sizeString=lengthString+"0";
			size=Integer.parseInt(sizeString)-Integer.parseInt(lengthString)-1;
		}
		
		return random.nextInt(size)+add;
	}
	
	/**
	 * 生成指定长度的数字和字母混合
	 * @param length
	 * @param state 0 大小写字母  1 大写字母 2 小写字母
	 * @return
	 */
	public  String getCharAndNumr(int length,int state) {
		String val = "";
		Random random = new Random();
		int choice=0;
		if(state==1){
			choice=65;
		}else if(state==2){
			choice=97;
		}else if(state!=0){
			state=0;
		}
		for (int i = 0; i < length; i++) {
			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
			if ("char".equalsIgnoreCase(charOrNum)) {
				if(state==0){
					choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
				} 
				
				val += (char) (choice + random.nextInt(26));
				
			} else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
				val += String.valueOf(random.nextInt(10));
			}
		}
		return val;
	}

	
	/**
	 * 根据主机名生成指定长度随机数
	 * @return
	 */
	public static  void getRandomOfOs(){
		String name=SystemUtil.getHostName();
		System.out.println("主机名："+name);
		String result=StringUtil.passAlgorithmsCiphering(name, StringUtil.MD5);
		System.out.println("md5值"+result);
		
		//获得算子
		BigInteger bigint=new BigInteger(result, 16);
	    String key=bigint.toString().substring(0,19);
	    
		Long count;
		try{
		    count=Long.parseLong(key);
		}catch(NumberFormatException e){
			key=1+key.substring(1);
			count=Long.parseLong(key);
		}
		
		//生成随机数
		Random r1 = new Random(count);
		System.out.println(r1.nextInt(10));
		
	}
	
	
	/**
	 * 获取cdrtype随机字符 A~Z，26位
	 * @return
	 */
	public static char getCdrTypeChar(){
		int num =random.nextInt(26);
		return (char)(65+num);
	}
	
	/**
	 * 获取随机位字符 0~9,A~Z,a~Z，62位
	 */
	public static char getRandomChar(){
		int num =random.nextInt(62);
		if (num>=0&&num<=9){
			return (char)(num+48);
		}else if(num>=10&&num<=35){ //A~Z
			return (char)(num+55);
		}else{  //a~z
			return (char)(num+61); 
		}
	}
	
	/**
	 * 多线程高效率生成随机整数数
	 * @param size 大于等于0 小于size
	 * @return
	 */
	public static int getThreadRandom(int size) {
		return ThreadLocalRandom.current().nextInt(size);
	}
}
